<script setup>
import {onMounted, ref} from "vue";
import {addCart, getAllProducts, getCollege, purchaseProduct} from "@/js/apiHelper";
import {timeCorrect} from "@/js/jsHelper";

  const products = ref([])
  onMounted(()=>{
    getCollegeName()
    appendProducts()
  })
  //查询大学
  const collegeName = ref()
  const getCollegeName = ()=>{
    const collegeResult = getCollege()
    collegeResult.then(res=>{
      collegeName.value = res.data
    })
  }
  //查询下一页
  const pageParams = ref({
    startIndex:-10,
    length:10
  })
  const appendProducts = ()=>{
    pageParams.value.startIndex += pageParams.value.length
    const productsResult = getAllProducts(pageParams.value.startIndex, pageParams.value.length)
    productsResult.then((res) => {
      if (res.data.length <= 0){
        pageParams.value.startIndex -= pageParams.value.length
        alert('没有更多数据了')
        return
      }
      res.data = res.data.filter((item) => item.status === 1)
      for (const datum of res.data) {
        datum.publishTime = timeCorrect(datum.publishTime)
        products.value.push(datum)
      }
    }).catch(e=>{
      console.log('获取失败'+e)
    })
  }
  //直接购买
  const contactSeller = (productId)=>{
    const result = purchaseProduct(productId)
    result.then(res=>{
      console.log(res)
      if (res.data === 0){
        alert('购买成功')
        products.value = products.value.filter((item) => item.productId !== productId)
      }else{
        switch (res.data){
          case -1:
            alert('未知错误')
                break;
          case -2:
            alert('手慢了，商品已被购买！')
            break;
          case -3:
            alert('不能购买自己的商品！')
            break;
        }
      }
    })
  }
  //加入购物车
  const addToCart = (productId)=>{
    const result = addCart(productId)
    result.then(res=>{
      console.log(res)
      if (res.data){
        alert('加入购物车成功')
        products.value = products.value.filter((item) => item.productId !== productId)
      }else{
        alert('加入购物车失败')
      }
    })
  }
  //刷新
  const refreshAll = ()=>{
    products.value = []
    pageParams.value.startIndex = -10
    setTimeout(()=>{
      appendProducts()
    },200)
  }
</script>

<template>
  <div class="appearing-container">
    <h1>闲置广场-{{collegeName}}</h1>
    <!--滚动-->
    <div class="scroll-container">
      <!--刷新-->
      <div class="refresh-btn" @click="refreshAll">刷新</div>
      <div v-for="(item, index) in products" :key="index" class="procuct-container">
        <!--背景-->
        <div class="block-back">
          <img :src="item.productImgs">
          <div></div>
        </div>
        <!--图片-->
        <div class="block-img">
          <img :src="item.productImgs">
        </div>
        <!--基础信息容器-->
        <div class="block-basic">
          <h2>{{ item.productPrice }}</h2>
          <h3>{{ item.productTitle }}</h3>
          <h4>发布时间：{{ item.publishTime }}</h4>
          <h4>地址：{{ item.location }}</h4>
          <button @click="contactSeller(item.productId)">直接购买</button>
          <button @click="addToCart(item.productId)">加入购物车</button>
        </div>
        <!--介绍容器-->
        <div class="block-intro">
          {{ item.productIntro }}
        </div>
      </div>
      <!--更多-->
      <div class="more-btn" @click="appendProducts">- 更多 -</div>
    </div>
  </div>
</template>

<style scoped>
  .appearing-container h1{
    position: absolute;
    top: 16px;
    left: 16px;
    right: 16px;
    font-size: 20px;
    font-weight: bold;
    letter-spacing: 4px;
    text-align: center;
  }
  .refresh-btn{
    position: fixed;
    right: 16px;
    top: 12px;
    font-size: 15px;
    text-decoration: underline;
    cursor: pointer;
  }
  .scroll-container{
    position: absolute;
    bottom: 20px;
    left: 16px;
    right: 16px;
    top: 60px;
    overflow-y: scroll;
    flex-wrap: wrap;
    display: flex;
    justify-content: space-between;
  }
  .scroll-container .procuct-container{
    width: calc(100%/2 - 8px);
    margin-bottom: 12px;
    height: 460px;
    border-radius: 16px;
    box-shadow: 2px 2px 6px #00000033;
    overflow: hidden;
    margin-right: 4px;
    position: relative;
    min-width: 600px;
  }
  /*背景*/
  .scroll-container .procuct-container .block-back{
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
  }
  .scroll-container .procuct-container .block-back div{
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: #fafafaaa;
    backdrop-filter: blur(4px);
  }
  .scroll-container .procuct-container .block-back img{
    min-width: 100%;
    min-height: 100%;
    position: absolute;
    object-fit: cover;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  /*图片*/
  .scroll-container .procuct-container .block-img{
    position: absolute;
    left: 12px;
    top: 12px;
    height: 270px;
    width: 360px;
    background-color: #fafafa;
    border-radius: 4px;
    border: #eee solid 2px;
    box-shadow: inset 0 0 8px #00000033;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
  }
  .scroll-container .procuct-container .block-img img{
    max-height: 100%;
    max-width: 100%;
    position: absolute;
    object-fit: contain;
  }
  /*基础信息*/
  .scroll-container .procuct-container .block-basic{
    position: absolute;
    left: 386px;
    right: 12px;
    top: 12px;
    height: 270px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }
  .scroll-container .procuct-container .block-basic h2{
    font-weight: bold;
    color: #5471c0;
    font-size: 42px;
    display: flex;
    align-items: start;
    margin-bottom: 0;
    position: relative;
  }
  .scroll-container .procuct-container .block-basic h2::after{
    content: "元";
    font-size: 22px;
    transform: translate(4px,14px);
  }
  .scroll-container .procuct-container .block-basic h3{
    font-size: 30px;
    text-overflow: clip;
    max-width: 100%;
    white-space: nowrap;
    font-weight: bold;
    color: #383838;
  }
  .scroll-container .procuct-container .block-basic h4{
    font-size: 16px;
    white-space: pre-line;
    max-width: 100%;
  }
  .scroll-container .procuct-container .block-basic button{
    padding: 8px 24px;
    outline: none;
    border: none;
    box-shadow: 2px 2px 4px #00000033;
    background: linear-gradient(to right bottom, #9bafe5, #bba1e3);
    color: #fff;
    cursor: pointer;
    border-radius: 4px;
    transition: .4s;
  }
  .scroll-container .procuct-container .block-basic button:hover{
    transform: rotateX(16deg);
    box-shadow: 4px 4px 12px #799be588;
    transition: .4s;
  }

  /*介绍信息*/
  .scroll-container .procuct-container .block-intro{
    position: absolute;
    top: 296px;
    bottom: 12px;
    left: 12px;
    right: 12px;
    padding: 8px;
    white-space: pre-line;
    overflow:scroll;
  }
  /*更多*/
  .more-btn{
    margin-left: 50%;
    transform: translateX(-50%);
    cursor: pointer;
  }
</style>